import utilfuncs from "../common/tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;

import globalConst from "../common/globalConst";
var global = globalConst.GlobalConst;

import { LGXEnums } from "../common/sysenum";
import lgxcom from '../common/common'
import { LineCoordCorrectSvc } from "../graph/action/correct/linecoordcorrectsvc";

const offsetgap = 30;

var CrossfyLineSvc = {}

CrossfyLineSvc.crossGraphConneLine = function (curentGraph, dataManager) {
    if (curentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
        return;
    }
    let curentRes = curentGraph.getTopoRes();
    if (!curentRes) {
        return;
    }
    let nodeList = curentRes.getConnNodeList();
    for (let i = 0; i < nodeList.length; i++) {
        let node = nodeList[i];
        if (node) {
            this.crossNodeConnLine(node, curentGraph, dataManager);
        }
    }
}

CrossfyLineSvc.crossNodeConnLine = function (node, curentGraph, dataManager) {
    let coordCorrectSvc = new LineCoordCorrectSvc(dataManager);
    let nodePt = node.getPos();
    let nLineCount = node.getConnLineCount();
    for (let n = 0; n < nLineCount; n++) {
        let lineRes = node.getConnLine(n);
        let lineGraph = lineRes.getRefGraph();
        if (lineGraph && lineGraph.getUseful() == 1) {
            if (!this.chkNeedReCrossfy(lineGraph, dataManager)) {
                continue;
            }
            let anotherEndConnGraph = this.getAnotherEndConnGraph(lineRes, curentGraph);
            coordCorrectSvc.startCrossConnLineGraph(nodePt,lineGraph,curentGraph,anotherEndConnGraph);
        }
    }
    coordCorrectSvc = null;
}



CrossfyLineSvc.getConflictGraphs = function (stPt, endPt, endConnGraph, dataManager) {
    let conflictGraphInfoList = [];
    let tAlllGraphList = dataManager.getAllGraphs();
    let count = tAlllGraphList.length;
    for (let i = 0; i < count; i++) {
        let pGraph = tAlllGraphList[i];
        if (pGraph.getUseful() != 1
            || pGraph.m_graphType == LGXEnums.LGXGraphType.MultiSel_Rect_Type
            || pGraph.m_graphType == LGXEnums.LGXGraphType.FastToolPanel_type
            || pGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
            continue;
        }
        if (endConnGraph && pGraph != endConnGraph && funcs.chkOverlap(endConnGraph.getZone(), pGraph.getZone())) {
            continue;
        }
        let rect = pGraph.getZone();
        let rst = funcs.getLineSecInterRectPoint(stPt, endPt, rect);
        if (rst && rst.flag) {
            let t = { graph: pGraph, dis: rst.dis, pt: rst.pt };
            conflictGraphInfoList.push(t);
        }
    }
    return conflictGraphInfoList;
}


CrossfyLineSvc.getAnotherEndConnGraph = function (lineRes, curentGraph) {
    let anotherEndConnRes = null;
    let curentRes = curentGraph.getTopoRes();
    let stNode = lineRes.stNode;
    let endNode = lineRes.endNode;
    if (stNode && endNode) {
        if (stNode.getRefRes() == curentRes) {
            anotherEndConnRes = endNode.getRefRes();
        }
        else if (endNode.getRefRes() == curentRes) {
            anotherEndConnRes = stNode.getRefRes();
        }
    }
    let destGraph = null;
    if (anotherEndConnRes) {
        destGraph = anotherEndConnRes.getRefGraph();
    }
    return destGraph;
}


CrossfyLineSvc.chkNeedReCrossfy = function (lineGraph, dataManager) {
    if (lineGraph.lineVisionType != LGXEnums.lineVisionType.cross) {
        return false;
    }
    let position = lineGraph.getPosCopy();
    let posList = position.posList;
    let prePt = posList[0];
    let curentPt = null;
    let needReCrossfy = false;
    let confilictOtherFlag = false;
    for (let i = 1; i < posList.length; i++) {
        curentPt = posList[i];
        let conflictGraphInfoList = this.getConflictGraphs(prePt, curentPt, null, dataManager);
        if (conflictGraphInfoList && conflictGraphInfoList.length > 0) {
            if (!this.chkIsLineConnGraph(conflictGraphInfoList, lineGraph)) {
                //检查冲突的图形是不是刚好是本线所连接的，如果不是则表示需要重新布局线的走向
                confilictOtherFlag = true;
                needReCrossfy = true;
                break;
            }
        }
        prePt = curentPt;
    }

    if (!confilictOtherFlag && !this.chkLineHornvert(position.posList)) {
        //如果没有与其他图形冲突，则检查线是否已经是横平竖直，如果是则不再自动布局
        needReCrossfy = true;
    }
    return needReCrossfy;
}

CrossfyLineSvc.chkLineHornvert = function (linePtList) {
    let haveSplashLineSec = false;
    let prePt = linePtList[0];
    let curentPt = null;
    for (let i = 1; i < linePtList.length; i++) {
        curentPt = linePtList[i];
        let detax = Math.abs(curentPt.x - prePt.x);
        let detay = Math.abs(curentPt.y - prePt.y);
        if (detax > global.ZERO_4E && detay > global.ZERO_4E) {
            haveSplashLineSec = true;
            break;
        }
        prePt = curentPt;
    }
    return !haveSplashLineSec;
}

CrossfyLineSvc.chkIsLineConnGraph = function (list, lineGraph) {
    let stNodeConnGraph = null;
    let endNodeConnGraph = null;
    if (lineGraph.getTopoRes()) {
        let stNode = lineGraph.getTopoRes().stNode;
        if (stNode && stNode.getRefRes()) {
            stNodeConnGraph = stNode.getRefRes().getRefGraph();
        }

        let endNode = lineGraph.getTopoRes().endNode;
        if (endNode && endNode.getRefRes()) {
            endNodeConnGraph = endNode.getRefRes().getRefGraph();
        }
    }

    if (!stNodeConnGraph && !endNodeConnGraph) {
        return false;
    }

    let haveOtherGraph = false;
    for (let i = 0; i < list.length; i++) {
        let info = list[i];
        if (info.graph != stNodeConnGraph && info.graph != endNodeConnGraph) {
            haveOtherGraph = true;
            break;
        }
    }
    return !haveOtherGraph;
}

export { CrossfyLineSvc }